package com.linewell.mobile.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import javax.websocket.Session;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.omg.CORBA.INTERNAL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import com.linewell.commom.model.SecondMarket;
import com.linewell.commom.service.ISecondMarketService;
import com.linewell.system.model.SysCustomer;

import com.linewell.system.pagemodel.ReturnInfo;
import com.linewell.system.utils.DateUtils;
import com.linewell.system.utils.MD5;



/**
 * 个人中心页面
 * 
 * @author 
 *
 */
@Controller
@EnableWebMvc
@SuppressWarnings("unchecked")
@RequestMapping(value="/mobile")
public class PersonalCenterController extends MobileBaseController{
	private static final Log log = LogFactory.getLog(PersonalCenterController.class);
	@Autowired
	private ISecondMarketService secondMarketService;
	
	@RequestMapping(value = "/gomy")
	public String gomy(HttpServletRequest request) {
		return super.index("/WEB-INF/pages/mobile/personalcenter/mainperson.jsp",request);
	}
	
	@RequestMapping(value = "/mySecond")
	public String mySecond(HttpServletRequest request) {
		return super.index("/WEB-INF/pages/mobile/personalcenter/my_second_info.jsp",request);
	}
	
	/**
	 * 个人资料  personalData
	 */
	@RequestMapping(value="/personalData")
	public String personalData(HttpServletRequest request){
		
 		List<SysCustomer> sysCustomer = null;
		HttpSession session = request.getSession();
		Object custName = session.getAttribute("CustName");
		System.out.println(custName);
		
		
		String hql = " from  SysCustomer o where o.status = 9 and o.custName = '"+custName+"'";
		sysCustomer = super.find(hql);
		session.setAttribute("sysCustomer",sysCustomer.get(0));

		return super.index("/WEB-INF/pages/mobile/personalcenter/personalData.jsp", request);
		
	}
	
	@ResponseBody
	@RequestMapping(value = "/savePerson")
	public boolean savePerson(SysCustomer c, HttpServletRequest request) {
		
				StringBuffer hql = new StringBuffer(
						"update SysCustomer s set ");
				//sysUserService.update(sysUser);
				//拼接修改条件
				if(c.getCustName() != null && !"".equals(c.getCustName())){
					hql.append("  s.custName = '"+c.getCustName()+"' ");
				}
				
				if(c.getPhone() != null && !"".equals(c.getPhone())){
				hql.append(" , s.phone = '"+c.getPhone()+"' ");
				}
				if(c.getPassword() != null && !"".equals(c.getPassword() )){
					hql.append(" , s.password = '"+MD5.md5(c.getPassword())+"' ");
				}
				
			    
				

				if(c.getStatus() != null && !"".equals(c.getStatus())){
					hql.append(" , s.status = '9' ");
				}

				hql.append(" where s.custId ="+c.getCustId()+" ");
				Query query = super.sessionFactory.getCurrentSession().createQuery(hql.toString());
				HttpSession session = request.getSession();
				session.removeAttribute("CustName");
				session.setAttribute("CustName", c.getCustName());
				return (query.executeUpdate() > 0);
			}
		
	@ResponseBody
	@RequestMapping(value = "/saveMySecond")
	public ReturnInfo savePerson(ReturnInfo info, @Valid SecondMarket secondMarket, HttpServletRequest request) {
		if (secondMarket != null) {
			try {
				//secondMarket.getCreateTime(DateUtils.getCreateTime());
				secondMarket.setFlag((byte)1);
				secondMarketService.save(secondMarket);

			} catch (Exception e) {
				info.setMsg("保存失败");
				info.setSuccess(1);
			}

		}
		return info;
	}
	
	
	
	
	
	
	
}
